<?php 
error_reporting(0);
header('Content-type: text/html; charset=UTF-8');
session_start();
if (empty($_SESSION['admin_username']) || empty($_SESSION['admin_id'])){
  header("location:/index.php");
  exit();
}

if (!empty($_FILES)){

    /* 设置允许上传文件的类型 */
    $type=array("gif","jpg","png","jpge");
    /* 获取上传文件信息 */
    $upfile=$_FILES['files'];
   
    $dest_dir=dirname(__FILE__)."/../upload/";

  

    /* 获取文件后缀名函数 */
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }

    /* 判断上传文件类型 */
    if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) )
    {
        echo "<script>alert('文件格式不正确！');</script>";
    }
    else
    {   
          $name = time().".".strtolower( fileext($upfile['name']) );
          $dest = $dest_dir.$name;
          /* 移动上传文件到指定文件夹 */
          $state=move_uploaded_file($upfile['tmp_name'],$dest);
 
          if ($state)
          {
          if  (empty($_POST['back'])){
           echo "<script>
window.opener.document.frm.".$_POST['input'].".value='".$name."';
window.close();
</script>";
          }else{
            echo "<script>
window.opener.".$_POST['back']."('".$name."');
window.close();
</script>";
          }
           
          }
          else
          {
           echo "<script>alert('上传失败，请重试！');</script>";
          }
          exit();
        
     }
 function creatdir($path)
{
if(!is_dir($path))
{
if(creatdir(dirname($path)))
{
mkdir($path,0777);
return true;
}
}
else
{
return true;
}
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片上传</title>
<style type="text/css">
<!--
td{font-size:12px}
a{color:#000000;text-decoration: none}
a:hover{text-decoration: underline}
.tx{height:16px;width:30px;border-color:black black #000000;border-top-width:0px;border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 12px; background-color: #eeeeee; color: #0000FF}
.button{font-size:12px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1{height:20px;width:30px;font-size:12px;border:1px solid;border-color:black black #000000;color: #0000FF}
-->
</style>
<script language="javascript">
<!--
function mysub()
{
    esave.style.visibility="visible";
}
-->
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="" enctype="multipart/form-data" >
  <div id="esave" style="position:absolute; top:18px; left:40px; z-index:10; visibility:hidden"> 
    <TABLE WIDTH=340 BORDER=0 CELLSPACING=0 CELLPADDING=0>
      <TR><td width=20></td>
  <TD bgcolor=#104A7B width="60"> 
  <TABLE WIDTH="340" height="120" BORDER=0 CELLSPACING=1 CELLPADDING=0>
  <TR> 
            <td bgcolor=#eeeeee align=center><font color=red>正在上传文件，请稍候...</font></td>
  </tr>
  </table>
  </td><td width=20></td>
  </tr></table></div>
  <table width="380" border="1" cellspacing="0" cellpadding="3" align="center" bordercolordark="#FFCC00" bordercolorlight="#FFCC00">
    <tr bgcolor="#FFCC00"> 
      <td height="22" align="left" valign="middle" bgcolor="#c1e1f9">&nbsp;图片上传 
      </td>
    </tr>
    <tr align="center" valign="middle"> 
      <td align="left" id="upid" height="70"> 选择文件: 
        <input type="file" name="files" style="width:300" class="tx1" value="" onchange="if(this.value!=''){javascript:mysub();document.form1.submit();}">
      </td>
    </tr>
    <input type="hidden" name="input" value="<?php echo $_GET['input'];?>">
    <input type="hidden" name="back" value="<?php echo $_GET['back'];?>">
    <tr align="center" valign="middle" bgcolor="#eeeeee"> 
      <td bgcolor="#c1e1f9" height="24"> 
        <input type="submit" name="Submit" value="· 开始上传 ·" class="button" onclick="javascript:mysub()">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
